package com.gs.SynPatrolRep.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.activity.palBill.PalBillSiteActivity;
import com.gs.SynPatrolRep.adapter.PalBillListAdapter;
import com.gs.SynPatrolRep.adapter.base.BaseAdapter;
import com.gs.SynPatrolRep.apiresult.ApiResultData;
import com.gs.SynPatrolRep.apiresult.PagedList;
import com.gs.SynPatrolRep.constant.AllConstants;
import com.gs.SynPatrolRep.constant.EventCode;
import com.gs.SynPatrolRep.control.LinearLayoutManager;
import com.gs.SynPatrolRep.db.bean.Event;
import com.gs.SynPatrolRep.db.dto.CancelPalBillDto;
import com.gs.SynPatrolRep.db.dto.PalBillListDto;
import com.gs.SynPatrolRep.db.dto.ShortDto;
import com.gs.SynPatrolRep.db.query.PalBillListQuery;
import com.gs.SynPatrolRep.dialog.CancelDialog;
import com.gs.SynPatrolRep.dialog.LoadingDialogUtils;
import com.gs.SynPatrolRep.fragment.base.BaseOptionListFragment;
import com.gs.SynPatrolRep.presenter.PalBillListPresenter;
import com.gs.SynPatrolRep.view.IPalBillListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class PalBillListFragment extends BaseOptionListFragment<IPalBillListView, PalBillListPresenter, PalBillListDto, PalBillListQuery> implements IPalBillListView {

    private PalBillListAdapter adapter;
    private String url;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_record;
    }

    @Override
    public PalBillListPresenter createPresenter() {
        return new PalBillListPresenter();
    }

    @Override
    public PalBillListQuery createQuery() {
        return new PalBillListQuery();
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        adapter = new PalBillListAdapter(context, list, R.layout.rvitem_patrol_list);
        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View v, int position) {
                PalBillListDto dto = list.get(position);
                Intent intent = new Intent(context, PalBillSiteActivity.class);
                intent.putExtra(AllConstants.ID, dto.getId());
                intent.putExtra(AllConstants.WHAT, dto.getCurState());
                startActivityForResult(intent, 1);
            }
        });
        adapter.setOnItemLongClickListener(new BaseAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClickListener(View v, int position) {
                PalBillListDto dto = list.get(position);
                new CancelDialog(context, R.string.cancel_patrol,dto.getBillNum(), new CancelDialog.IDoneListener() {
                    @Override
                    public void getDoneFromDialog(String remark) {
                        CancelPalBillDto bean = new CancelPalBillDto();
                        bean.setId(dto.getId());
                        bean.setRemark(remark);
                        mPresenter.cancel(bean, position);
                    }
                }).show();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void initData() {
        query.init();
        query.setKeyWord(toolBarSearch.getText());
        url = "/api/palBills/all";
        mPresenter.getData(query,url);
    }

    @Override
    public void loadData() {
        mPresenter.getData(query,url);
    }

    @Override
    public void loadOptionData() {
        List<ShortDto> listShort = new ArrayList<>();
        listShort.add(new ShortDto("/api/palBills/awaitPals", getString(R.string.beInspected)));
        listShort.add(new ShortDto("/api/palBills/awaitApprovals", getString(R.string.inspected)));
        toolBarOption.setList(listShort);
    }

    @Override
    public void add() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        if (event.getWhat() == EventCode.EVENT_JUMP) {
            if (event.getCurPos() == 0) {
                toolBarOption.changeState(1);
            }else {
                toolBarOption.changeState(2);
            }
        }
    }

    @Override
    public void search() {
        list.clear();
        adapter.notifyDataSetChanged();
        query.init();
        query.setKeyWord(toolBarSearch.getText());
        mPresenter.getData(query,url);
    }

    @Override
    public void optionItem(Object option) {
        if (option == null) return;
        ShortDto dto = (ShortDto) option;
        url = dto.getId().isEmpty()?"/api/palBills/all":dto.getId();
        search();
    }

    @Override
    public void delete(int position) {

    }

    @Override
    public void update(int position) {

    }

    @Override
    public void onGetDataSuccess(ApiResultData<PagedList<PalBillListDto>> apiResult) {
        refreshSmartRefreshLayout();
        if (apiResult.getStatus() != 2000) {
            Toast.makeText(context, apiResult.getMessage(), Toast.LENGTH_LONG).show();
            adapter.notifyDataSetChanged();
            return;
        }
        list.addAll(apiResult.getData().getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onGetDataFails(Exception e) {
        refreshSmartRefreshLayout();
        adapter.notifyDataSetChanged();
        Toast.makeText(context, R.string.network_error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCancelSuccess(ApiResultData<String> apiResult, int position) {
        Toast.makeText(context, apiResult.getMessage(), Toast.LENGTH_LONG).show();
        if (apiResult.getStatus() == 2000) {
            adapter.delete(position);
        }
    }

    @Override
    public void onCancelFails(Exception e) {
        Toast.makeText(context, R.string.network_error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) {
            list.clear();
            query.init();
            mPresenter.getData(query,url);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(context);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }
}
